package com.codetree.strategy.functionalInterface;

import com.codetree.strategy.functionalInterface.domain.PaymentResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

@Service
public class PaymentFunctionService {


    private final Map<String, PaymentFunctionStrategy> strategies;

    // Spring 自动注入所有 PaymentStrategy 类型的 Bean，Key 为 Bean 名称

    @Autowired
    public PaymentFunctionService(Map<String, PaymentFunctionStrategy> strategies) {
        this.strategies = strategies;
    }

    public PaymentResult executePayment(String type, BigDecimal amount) {
        PaymentFunctionStrategy strategy = strategies.get(type);
        if (strategy == null) {
            return new PaymentResult(Boolean.FALSE, UUID.randomUUID().toString(), "不支持的支付类型: " + type);
        }
        return strategy.pay(amount);
    }
}
